package com.zhao.pay;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom.JDOMException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhao.util.XMLUtil;
 
/**
 * 微信支付成功回调地址
 * @author zhaowj
 * @date2018年3月14日 下午9:48:44
 */
@WebServlet("/PaySuessServlet")
public class PaySuessServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger log=LoggerFactory.getLogger(PaySuessServlet.class);
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		InputStream inStream=request.getInputStream();
		ByteArrayOutputStream outStream=new ByteArrayOutputStream();
		byte[] buffer=new byte[1024];
		int len=0;
		while ((len=inStream.read(buffer))!=-1) {
			outStream.write(buffer,0,len);
		}
		
		log.debug("~~~~~~~~~~~~~~~~~~~PaySuesssssssssssssssssssssss");
		outStream.close();
		inStream.close();
		String result=new String(outStream.toByteArray(),"utf-8");
		Map<Object, Object> map=null;
		
		try {
			map=XMLUtil.doXMLParse(result);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(map!=null)
		{
			//货币种类
			String fee_type=map.get("fee_type").toString();
			//商户单号
			String out_trade_no=map.get("out_trade_no").toString();
			//交易单号
			String transaction_id=map.get("transaction_id").toString();
			//交易完成时间(yyyyMMddhhmmss)
			String time_end=map.get("time_end").toString();
			//用户唯一标识
			String openid=map.get("openid").toString();
			//银行类型
			String bank_type=map.get("bank_type").toString();
			//订单金额(单位:分)
			String total_fee=map.get("total_fee").toString();
			
			if(map.get("result_code").toString().equalsIgnoreCase("SUCCESS"))
			{
				//此处写数据库方法
				
				
				response.getWriter().write(MysteriousUtil.setXML("SUCCESS", ""));
			}
		}
	}

	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
